// pages/send-out-goods/send-out-goods.js
var util = require('../../utils/util.js')
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var orderId = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tc: 0,
    uid: 0,
    token: '',
    openid: '',
    deliveName:[],
    actionSheetHidden: true,
    selectedDeliveItem: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    orderId = options.orderid;
    var that = this;
    that.setData({
      tc: app.get_tc()
    })
    this.getDeliverName();
    //当前登录用户信息
    app.LoginUserInfo(function (res) {
      //更新数据
      that.setData({
        uid: res.uid,
        token: res.token,
        openid: res.wxsmall_openid
      });
    });
  },

  //获取快递 名称
  getDeliverName: function () {
    var that = this;
    var params = {
      publickey: util.publiccode,
      ac: "getexpress",
    };
    util.api({
      data: params,
      method: 'GET',
      success: function (responseData) {
        var resData = responseData.data;
        console.log("快递名称：", resData);
        if (resData.msg) {
          app.dialog(resData.msg, 'loading');
        } else {
          that.setData({
            deliveName: resData
          });
        }
      }
    });
  },

  showSelect:function(){
    this.setData({
      actionSheetHidden: false
    });
  },

  selectAction: function(event){
    var value = event.currentTarget.dataset.link;
    this.setData({
      selectedDeliveItem: value,
      actionSheetHidden: true
    });
  },
  actionSheetTap: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },

  //发货
  sendOut:function(e) {
    if (!this.data.selectedDeliveItem) {
      app.dialog("请选择快递", 'loading');
      return;
    }
    var deliveNo = e.detail.value.deliveNo;
    if (!deliveNo) {
      app.dialog("请输入快递单号", 'loading');
      return;
    }
    var that = this;
    console.log("uid = " + this.data.uid + "; token = " + this.data.token);
    var privatekey = utilMd5.hexMD5(this.data.uid + 'sendgoods' + this.data.token);
    var params = {
      ac: "sendgoods",
      uid: that.data.uid,
      privatekey: privatekey,
      "orderid": orderId,
      "expid": that.data.selectedDeliveItem.id,
      "expnum": deliveNo
    };
    console.log("发货：", params);
    util.api({
      data: params,
      method: 'POST',
      success: function (responseData) {
        var resData = responseData.data;
        console.log("快递名称：", resData);
        if (resData.res) {
          app.dialogToast("发货成功", 'loading', function(){
            wx.navigateBack({
              delta: 1
            })
          });
        } else {
          app.dialog(resData.msg, 'loading');
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

})